<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
        <!-- 引入vue.js -->
    <script src="js/vue.js"></script>
    <style>
        #box ul{
            list-style: none;
            width: 60%;
            margin: auto;
            border: 1px solid black;
            padding: 10px;
        }
        #box ul li{
            display: flex;
            justify-content: space-around;
            margin-top: 10px;
            border-bottom: 1px dashed #cccccc;
        }
        #box ul li:last-child{
            border-bottom: none;
        }
        #box ul li img{
            width: 100px;
        }
        .all{
            width: 60%;
            margin: auto;
        }
    </style>
</head>
<body>
    <div id="box">
        <div class="all" v-if="list.length === 0">购物车空空如也</div>
        <template v-else>
            <div class="all"><input type="checkbox" v-model="isChecked" @change="allCheck()">全选|全不选</div>
                <ul>
                    <li v-for="(item,index) in list" :key="item.id">
                        <div>
                            <input type="checkbox" v-model="checkGroup" :value="item" @change="isAllCheck()"/>
                        </div>
                        <div>
                            <img :src="item.pic" alt="">
                        </div>
                        <div>
                            <div>名称:{{item.name}}</div>
                            <div>价格：￥{{item.price}}</div>
                        </div>
                        <div>
                            <button @click="item.number--" :disabled="item.number===1">-</button>
                            <span>{{item.number}}</span>
                            <button @click="item.number++" :disabled="item.number===item.limit">+</button>
                        </div>
                        <div>
                            <button @click="deleteGood(index,item.id)">删除</button>
                        </div>
                    </li>
                </ul>
                <div class="all">总金额: {{totalPrice()}}</div>
        </template>      
    </div>
    <script>
        var vm = new Vue({
            el:"#box",
            data:{
                isChecked:false,
                checkGroup:[],
                list:[
                    {
                        name:"商品1",
                        price:10,
                        number:1,
                        id:1,
                        limit:5,//限购
                        pic:"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1689157983,3552298815&fm=26&gp=0.jpg"
                    },
                    {
                        name:"商品2",
                        price:20,
                        number:5,
                        id:2,
                        limit:8,//限购
                        pic:"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1689157983,3552298815&fm=26&gp=0.jpg"
                    },
                    {
                        name:"商品3",
                        price:30,
                        number:10,
                        id:3,
                        limit:15,//限购
                        pic:"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1689157983,3552298815&fm=26&gp=0.jpg"
                    },
                    {
                        name:"商品4",
                        price:8,
                        number:6,
                        id:4,
                        limit:18,//限购
                        pic:"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1689157983,3552298815&fm=26&gp=0.jpg"
                    },
                    {
                        name:"商品5",
                        price:14,
                        number:2,
                        id:5,
                        limit:7,//限购
                        pic:"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1689157983,3552298815&fm=26&gp=0.jpg"
                    }
                ]
            },
            methods:{
                totalPrice(){
                    var total = 0;
                    this.checkGroup.forEach((ele)=>{
                        total += ele.price * ele.number;
                    })
                    return total
                },
                //删除商品
                deleteGood(index,goodId){
                    //删除原商品
                    this.list.splice(index,1);
                    //更新选中的商品
                    this.checkGroup = this.checkGroup.filter(ele => ele.id !== goodId)
                    //删除单个商品时也要检测是否全部被选中
                    this.isAllCheck();
                },
                //全选全不选
                allCheck(){
                    if(this.isChecked){
                        this.checkGroup = this.list
                    }else{
                        this.checkGroup = [];
                    }
                },
                //选择单个商品时，判断是否为全部选中
                isAllCheck(){
                    if(this.checkGroup.length === this.list.length){
                        this.isChecked = true;
                    }else{
                        this.isChecked = false;
                    }
                }
            }
        })
    </script>
</body>
</html>